//
//  WSTextFieldAlterController.m
//  CharonTVProject
//
//  Created by karong on 2023/6/15.
//

#import "WSTextFieldAlterController.h"

@interface WSTextFieldAlterController () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *inputView;
@property (weak, nonatomic) IBOutlet UITextField *tf;
@property (weak, nonatomic) IBOutlet UILabel *textLenthLabel;


@end

@implementation WSTextFieldAlterController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.tf.text = self.contentStr;
    [self resetTextLengthWithLength:self.contentStr.length];
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.view.backgroundColor = UIColor.mask_black_50p;
    self.tf.tintColor = UIColor.font_yellow_FCB625;
    [self.tf becomeFirstResponder];
}

- (IBAction)cancelAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        if (self.cancelHandle) {
                self.cancelHandle();
            }
    }];
}

- (IBAction)confirmAction:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        if (self.confirmHandle) {
            self.confirmHandle(self.tf.text);
        }
    }];
}

#pragma mark UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([string isEqualToString:@""]) {
        [self resetTextLengthWithLength:textField.text.length - 1];
        return YES;
    }
    if (range.length == 1 && string.length == 0) {
        [self resetTextLengthWithLength:textField.text.length + string.length];
        return YES;
    }
    else if (textField.text.length + string.length >= self.maxWordCount) {
        NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
        textField.text = [newText substringToIndex:MIN(newText.length, self.maxWordCount)];
        [self resetTextLengthWithLength:textField.text.length];
        return NO;
    }
    [self resetTextLengthWithLength:textField.text.length + string.length];
    return YES;
}

- (void)resetTextLengthWithLength:(NSInteger)length {
    NSString * str = WSSF(@"%ld/%ld",(long)length, (long)self.maxWordCount);
    NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithString:str
                                                                                 attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:12], NSForegroundColorAttributeName: UIColor.font_black_333333}];
    NSRange range = NSMakeRange(str.length - WSSF(@"%ld", (long)self.maxWordCount).length, WSSF(@"%ld", (long)self.maxWordCount).length);
    [attrText addAttributes:@{NSForegroundColorAttributeName: [UIColor colorFromHexRGB:@"e0e0e0"]} range:range];
    self.textLenthLabel.attributedText = attrText;
}

//- (void)setContentStr:(NSString *)contentStr {
//    [super setContentStr:contentStr];
//    self.tf.text = contentStr;
//    [self resetTextLengthWithLength:self.contentStr.length];
//}

- (void)setPlaceholderStr:(NSString *)placeholderStr {
    _placeholderStr = placeholderStr;
    self.tf.placeholder = placeholderStr;
}
@end
